package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.RoomProduct;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 房间商品（未用）
 */
@Component
public class RoomProductDao implements BaseDao<RoomProduct> {

    @Override
    public int insert(RoomProduct entity) {
        String sql = "insert into room_product (room_product_type_id,product_id, price,is_deleted) values (?,?,?,0)";
        return DBHelper.executeUpdate(sql, entity.getRoom_product_type_id(), entity.getProduct_id(), entity.getPrice());
    }

    @Override
    public int update(RoomProduct entity) {
        String sql = "update room_product set room_product_type_id=?, product_id=?, price=? where id=?";
        return DBHelper.executeUpdate(sql, entity.getRoom_product_type_id(), entity.getProduct_id(), entity.getPrice(),
            entity.getId());
    }

    @Override
    public int delete(RoomProduct entity) {
        String sql = "update room_product set is_deleted=1 where id=?";
        return DBHelper.executeUpdate(sql, entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        String sql =
            "select * from room_product rp inner join room_product_type rpt" + " on rp.room_product_type_id = rpt.id "
                + "inner join product p on rp.product_id=p.id where rp.is_deleted=0";
        return DBHelper.executeQuery(sql);
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        RoomProduct room_product = (RoomProduct)obj[0];
        String sql =
            "select rp.id,rpt.id rptid,rpt.name rptname,p.id pid, p.name pname,rp.price from room_product rp inner join room_product_type rpt"
                + " on rp.room_product_type_id = rpt.id "
                + " inner join product p on rp.product_id=p.id where 1=1 and rp.is_deleted=0 ";
        if (room_product.getRoom_product_type_id() != -1 && !"".equals(room_product.getRoom_product_type_id())) {
            sql += " and rpt.id = " + room_product.getRoom_product_type_id();
        }
        return DBHelper.executeQuery(sql + " ORDER BY rp.id desc limit ?,?", page.getBegin(), page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(RoomProduct entity) {
        String sql =
            "select rp.id,rp.room_product_type_id,rp.product_id,rpt.name rptname, p.name pname,rp.price from room_product rp inner join room_product_type rpt"
                + " on rp.room_product_type_id = rpt.id "
                + " inner join product p on rp.product_id=p.id where rp.id=? and rp.is_deleted=0 ";
        return DBHelper.executeQuery(sql, entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        RoomProduct room_product = (RoomProduct)obj[0];
        String sql = "select count(*) counts from room_product rp inner join room_product_type rpt"
            + " on rp.room_product_type_id = rpt.id "
            + "inner join product p on rp.product_id=p.id where 1=1 and rp.is_deleted=0 ";
        if (room_product.getRoom_product_type_id() != -1 && !"".equals(room_product.getRoom_product_type_id())) {
            sql += " and rp.room_product_type_id =" + room_product.getRoom_product_type_id() + "";
        }
        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }

    // 外键表 room_product_type
    public List<Map<String, Object>> findRoom_product_type() {
        String sql = "select * from room_product_type where is_deleted=0";
        return DBHelper.executeQuery(sql);
    }

    // 外键表 product
    public List<Map<String, Object>> findProduct() {
        String sql = "select * from product where is_deleted=0";
        return DBHelper.executeQuery(sql);
    }
}
